package leetcode.list;

import java.math.BigInteger;
import java.util.LinkedList;

public class LcLCR025 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        LinkedList<Integer> stack1 = new LinkedList<>();
        LinkedList<Integer> stack2 = new LinkedList<>();
        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            stack2.push(l2.val);
            l2 = l2.next;
        }
        LinkedList<Integer> stack3 = new LinkedList<>();
        int carry = 0;
        while (!stack1.isEmpty() && !stack2.isEmpty()) {
            int sum = carry + stack1.pop() + stack2.pop();
            stack3.push(sum % 10);
            carry = sum / 10;
        }
        while (!stack1.isEmpty()) {
            int sum = carry + stack1.pop();
            stack3.push(sum % 10);
            carry = sum / 10;
        }
        while (!stack2.isEmpty()) {
            int sum = carry + stack2.pop();
            stack3.push(sum % 10);
            carry = sum / 10;
        }
        if (carry != 0) stack3.push(carry);
        ListNode dummyHead = new ListNode(-1);
        ListNode preNode = dummyHead;
        while (!stack3.isEmpty()) {
            ListNode node = new ListNode(stack3.pop());
            preNode.next = node;
            preNode = node;
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        ListNode n10 = new ListNode(9, null);
        ListNode n9 = new ListNode(9, n10);
        ListNode n8 = new ListNode(9, n9);
        ListNode n7 = new ListNode(9, n8);
        ListNode n6 = new ListNode(9, n7);
        ListNode n5 = new ListNode(9, n6);
        ListNode n4 = new ListNode(9, n5);
        ListNode n3 = new ListNode(9, n4);
        ListNode n2 = new ListNode(9, n3);
        ListNode n1 = new ListNode(3, n2);

        ListNode n0 = new ListNode(7);
        LcLCR025 lcLCR025 = new LcLCR025();
        lcLCR025.addTwoNumbers(n1,n0);


        BigInteger x = new BigInteger("9");
        x.add(new BigInteger("1"));
        System.out.println(x.add(new BigInteger("1")));


    }

}
